package com.heatingcharge.controller.unit;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.util.R;
import com.heatingcharge.dto.ResBuildingDTO;
import com.heatingcharge.dto.ResUnitDTO;
import com.heatingcharge.entity.BaseEntity;
import com.heatingcharge.entity.unit.ResBuilding;
import com.heatingcharge.entity.unit.ResUnit;
import com.heatingcharge.param.ResBuildingParam;
import com.heatingcharge.service.unit.IResBuildingService;
import com.heatingcharge.service.unit.IResUnitService;
import com.heatingcharge.vo.TreeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 楼  前端控制器
 * </p>
 *
 * @author devin.jiang
 * @since 2019-07-12
 */
@RestController
@RequestMapping("/res-building")
public class ResBuildingController {
    @Autowired
    IResBuildingService resBuildingService;
    @Autowired
    IResUnitService unitService;

    @ApiOperation(value = "分页获取楼栋信息",notes = "分页获取楼栋信息")
    @GetMapping
    public R getBuilding(Page page, ResBuildingDTO resBuildingDTO){
        IPage<ResBuilding> buildingPage = resBuildingService.selectBuildingPage(page, resBuildingDTO);
        return R.ok(buildingPage);
    }

    @GetMapping("/{buildId}/res-unit")
    public R getUnitByBuildingId(ResUnitDTO resUnitDTO){
        List<ResUnit> resUnits = unitService.selectResUnit(resUnitDTO);
        return R.ok(resUnits);
    }

    @GetMapping("/tree")
    public R getBuildingTree(ResBuildingDTO resBuildingDTO){
        resBuildingDTO.setShowUnit(true);
        resBuildingDTO.setShowStatistics(true);
        List<TreeVo> treeVos = resBuildingService.selectBuildingTree(resBuildingDTO);
        return R.ok(treeVos);
    }

  @ApiOperation(value = "新增楼栋",notes = "新增楼栋")
    @PostMapping
    public R saveBuilding(@RequestBody ResBuildingParam resBuildingParam){
        if (resBuildingService.save(resBuildingParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "更新楼栋",notes = "更新楼栋")
    @PutMapping
    public R updateBuilding(@RequestBody ResBuildingParam resBuildingParam){
        if (resBuildingService.update(resBuildingParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "删除楼栋",notes = "根据主键ID删除楼栋")
    @DeleteMapping("/{id}")
    public R delBuilding(@PathVariable("id") Integer id){

        ResBuilding resBuilding=ResBuilding.builder()
                .id(id)
                .is_deleted(1)
                .build();
        if (resBuildingService.updateById(resBuilding)){
            return R.ok();
        }
        return R.error();
    }
}

